﻿using Cognitum.CassandraProviders;
using Cognitum.CassandraProviders.Cassandra;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;

namespace Cognitum.ProvidersTests
{
    [TestFixture]
    internal class BaseProvidersTest
    {
        #region | Fields |

        protected CassandraMembershipProvider _providerMembership;
        protected CassandraProfileProvider _providerProfile;
        protected CassandraRoleProvider _providerRole;
        protected CassandraSessionStateProvider _providerSession;

        protected string _applicationName = "MyApp1";
        protected NameValueCollection _conf;
        protected string _email = "email14@a.pl";
        protected bool _isApproved = true;
        protected string _password = "Adadw32";
        protected string _passwordAnswer = "3";
        protected string _passwordQuestion = "Why";
        protected object _providerUserKey = "_providerUserKey";
        protected MembershipCreateStatus _status;
        protected string _userName = "john";
        protected string _userNameWrong = "wrongusername";

        protected string _roleReader = "reader";
        protected string _roleWriter = "writer";

        #endregion | Fields |

        [SetUp]
        public virtual void Init()
        {
            _conf = new NameValueCollection();
            _conf.Add("applicationName", _applicationName);
            _conf.Add("maxInvalidPasswordAttempts", "5");
            _conf.Add("passwordAttemptWindow", "10");
            _conf.Add("minRequiredNonAlphanumericCharacters", "1");
            _conf.Add("minRequiredPasswordLength", "7");
            _conf.Add("passwordStrengthRegularExpression", "");
            _conf.Add("enablePasswordReset", "true");
            _conf.Add("enablePasswordRetrieval", "true");
            _conf.Add("requiresQuestionAndAnswer", "false");
            _conf.Add("requiresUniqueEmail", "true");
            _conf.Add("passwordFormat", "Clear");
            _conf.Add("name", "CustomMembershipProvider");
            _conf.Add("ConnectionString", "localhost");
            _conf.Add("AdminName", "Admin");
            _conf.Add("UserIsActiveTimeWindow", "3");
            _conf.Add("ReplicationFactor", "4");
            _providerMembership = new CassandraMembershipProvider();
            _providerMembership.Initialize("CustomMembershipProvider", _conf);
            _providerRole = new CassandraRoleProvider();
            _providerRole.Initialize("CustomRoleProvider", _conf);

            try
            {
                DeleteUser(_userName);
            }
            catch { }
            try
            {
                DeleteUser("userdName");
            }
            catch { }
            try
            {
                DeleteUser("userdName1");
            }
            catch { }
            try
            {
                DeleteUser("userdName2");
            }
            catch { }
            try
            {
                _providerRole.DeleteRole(_roleWriter, false);
            }
            catch { }
            try
            {
                _providerRole.DeleteRole(_roleReader, false);
            }
            catch { }
            try
            {
                _providerRole.DeleteRole("test", false);
            }
            catch { }
        }

        protected void AddDefaultUser(string userName, string email)
        {
            _providerMembership.CreateUser(
                userName,
                _password,
                email,
                _passwordQuestion,
                _passwordAnswer,
                _isApproved,
                _providerUserKey,
                out _status
                );
        }

        protected void DeleteUser(string userName)
        {
            _providerMembership.DeleteUser(userName, true);
        }
    }
}